import sys
import string
import types
from xml.sax import saxexts
from xml.sax import saxlib

class VerySimpleHandler(saxlib.DocumentHandler):

   def startElement(self, name, attrs):
      print "startElement(%s, %s)" % (name, attrs)
      for i in range(0, len(attrs)):
      	print " - (%s, %s)" % (attrs.getName(i), attrs.getValue(i))

   def endElement(self, name):
      print "endElement(%s)" % name

   def characters(self, ch, start, length):
      print "character data %s %d %d" % (ch, start, length)
      print " ->",ch[start:start+length]
      print "char data"

vsh = VerySimpleHandler()
parser = saxexts.make_parser()
parser.setDocumentHandler(vsh)
try:
    f = open(sys.argv[1])
    infile = f.readlines()
    #Since the file contains <br>'s.  This section will split the
    #lines into a list and remove the <br>
    tempFile = ""
    newFile = ""
    i = 0
    while(i < len(infile)):
	newList = infile[i].split('<br>')
	for item in newList:
		tempFile = tempFile + item
	i = i + 1
    #Removing the &nbsp from file
    tempFile = tempFile.split('&nbsp')
    for item in tempFile:
	newFile = newFile + item
    #Opening the temp file to write the new xml info to
    myFile = open('tempFile.xml','w')
    myFile.write(newFile)
    myFile.close()
    #Opening the file to give to the parser
    myFile = open('tempFile.xml','r')
    parser.parseFile(myFile)
except IOError,e:
    print "\nI/O Error: " + document_uri + ": " + str(e)
except saxlib.SAXException,e:
    print "\nParse Error: " + document_uri  + ": " + str(e)
except:
    print "Thank you for riding the CTA Brown Line."


